package com.cn.xact.ty_treasured_book.activity;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.R.layout;
import com.cn.xact.ty_treasured_book.R.menu;
import com.cn.xact.ty_treasured_book.activity.LoginActivity.MyNetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class RegisterActivity extends Activity {

    private NetworkImpl mRegisterNetwork;
    private EditText mAccount = null;
    private EditText mPassword = null;
    private CheckBox mDisplayPassword = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_register);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.arrow_left);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);

        title1.setPadding((width - tmp.getMeasuredWidth() - title1.getMeasuredWidth())/2, 0, 0, 0);
        title1.setText(R.string.register);

        tmp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                RegisterActivity.this.finish();
            }

        });

        mAccount = (EditText)findViewById(R.id.register_account);
        mPassword = (EditText)findViewById(R.id.register_password);

        mRegisterNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        Button loginBt = (Button)findViewById(R.id.register_confirm);
        loginBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String account = mAccount.getText().toString();
                String password = mPassword.getText().toString();

                if (account.length() <= 0) {
                    UIHelper.showConfirmDialog(arg0.getContext(), null, getResources().getString(R.string.login_invalid_account));
                } else if (password.length() <= 0) {
                    UIHelper.showConfirmDialog(arg0.getContext(), null, getResources().getString(R.string.login_invalid_password));
                } else {
                    mRegisterNetwork.registerAsync(account, password);
                }
            }

        });

        mDisplayPassword = (CheckBox)findViewById(R.id.register_show_password);
        mDisplayPassword.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                if (arg1) {
                    mPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    mPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.register, menu);
        return true;
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(RegisterActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_REGISTER:
                RegisterActivity.this.finish();
                break;
            }
        }
    }

}
